﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using AudioPlayer.Entity;
namespace AudioPlayer.GUI.List {
	internal class Header : BaseEntity {
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the Header class.
		/// </summary>
		/// <param name="text">The Text.</param>
		/// <param name="type">The Type.</param>
		public Header(string text, HeaderType type) {
			Init(text, type, 60, false);
		}
		/// <summary>
		/// Initializes a new instance of the Header class.
		/// </summary>
		/// <param name="text">The Text.</param>
		/// <param name="type">The Type.</param>
		/// <param name="width">The Width.</param>
		public Header(string text, HeaderType type, int width) {
			Init(text, type, width, false);
		}
		/// <summary>
		/// Initializes a new instance of the Header class.
		/// </summary>
		/// <param name="text">The Text.</param>
		/// <param name="type">The Type.</param>
		/// <param name="max">if set to <c>true</c> headers becomes maximum.</param>
		public Header(string text, HeaderType type, bool max) {
			Init(text, type, 0, max);
		}
		#endregion
		#region Private Voids
		/// <summary>
		/// Inits instance
		/// </summary>
		/// <param name="text">The Text.</param>
		/// <param name="type">The Type.</param>
		/// <param name="width">The Width.</param>
		/// <param name="max">if set to <c>true</c> header becomes maximum.</param>
		private void Init(string text, HeaderType type, int width, bool max) {
			this.Text = text;
			this.Width = width;
			this.Max = max;
			this.Type = type;
		}
		#endregion
		#region Public Voids
		/// <summary>
		/// Calculates if needs to show resize cursor
		/// </summary>
		/// <param name="p">Location of mouse</param>
		/// <returns></returns>
		public bool ShowResize(Point p) {
			Rectangle rect = new Rectangle(this.Bounds.X + this.Bounds.Width - 2, this.Bounds.Y, 2, this.Bounds.Height);
			if (rect.Contains(p)) {
				return true;
			} else {
				return false;
			}
		}
		#endregion
		#region Override
		/// <summary>
		/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
		/// </returns>
		public override string ToString() {
			return this.Text;
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets or sets the text.
		/// </summary>
		/// <value>The text.</value>
		public string Text {
			get { return this.GetValue<string>("Text"); }
			set { this.SetValue<string>("Text",value); }
		}
		/// <summary>
		/// Gets or sets the width.
		/// </summary>
		/// <value>The width.</value>
		public int Width {
			get { return this.GetValue<int>("Width"); }
			set { this.SetValue<int>("Width",value); }
		}
		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="Header"/> is visible.
		/// </summary>
		/// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
		public bool Visible {
			get { return this.GetValue<bool>("Visible"); }
			set { this.SetValue<bool>("Visible",value); }
		}
		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="Header"/> is maximum header.
		/// </summary>
		/// <value><c>true</c> if is maximum; otherwise, <c>false</c>.</value>
		public bool Max {
			get { return this.GetValue<bool>("Max"); }
			set { this.SetValue<bool>("Max",value); }
		}
		/// <summary>
		/// Gets or sets the bounds.
		/// </summary>
		/// <value>The bounds.</value>
		public Rectangle Bounds {
			get { return this.GetValue<Rectangle>("Bounds"); }
			set { this.SetValue<Rectangle>("Bounds",value); }
		}
		/// <summary>
		/// Gets or sets the header type.
		/// </summary>
		/// <value>The type.</value>
		public HeaderType Type {
			get { return this.GetValue<HeaderType>("Type"); }
			set { this.SetValue<HeaderType>("Type",value); }
		}
		/// <summary>
		/// Gets or sets the maximum.
		/// </summary>
		/// <value>The maximum.</value>
		public int Maximum {
			get { return this.GetValue<int>("Maximum"); }
			set {
				if (value > this.Maximum) {
					this.SetValue<int>("Maximum", value);
				}
			}
		}
		/// <summary>
		/// Gets or sets a value indicating whether mouse is over this header
		/// </summary>
		/// <value><c>true</c> if hover; otherwise, <c>false</c>.</value>
		public bool Hover {
			get { return this.GetValue<bool>("Hover"); }
			set { this.SetValue<bool>("Hover",value); }
		}
		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="Header"/> is drawed.
		/// </summary>
		/// <value><c>true</c> if drawed; otherwise, <c>false</c>.</value>
		public bool Drawed {
			get { return this.GetValue<bool>("Drawed"); }
			set { this.SetValue<bool>("Drawed",value); }
		}
		#endregion
	}
	
	internal class HeaderCollection : List<Header> {
		#region Variables
		private int _maxWidth;
		#endregion
		#region Public Voids
		public void Resize() {
			Header _max = null;
			for (int i = 0; i < this.Count; i++) {
				if (this[i].Max) {
					_max = this[i];
					_max.Width = 20;
				}
			}
			if (_max != null) {
				_max.Width = this.MaxWidth - this.Width + 20;
			}
			Rebound();
		}
		public void Rebound() {
			int x = 0;
			for (int i = 0; i < this.Count; i++) {
				this[i].Bounds = new Rectangle(x, 0, this[i].Width, this.Height);
				x += this[i].Width;
			}
		}

		public Header HitInfo(Point p) {
			for (int i = 0; i < this.Count; i++) {
				if (this[i].Bounds.Contains(p)) {
					return this[i];
				}
			}
			return null;
		}
		public void UnHover() {
			for (int i = 0; i < this.Count; i++) {
				Header h = this[i];
				h.Hover = false;
			}
		}
		public void UnDrawed() {
			for (int i = 0; i < this.Count; i++) {
				Header h = this[i];
				h.Drawed = false;
			}
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets the width.
		/// </summary>
		/// <value>The width.</value>
		public int Width {
			get {
				int w = 0;
				for (int i = 0; i < this.Count; i++) {
					w += this[i].Width;
				}
				return w;
			}
		}
		public int MaxWidth {
			get { return _maxWidth; }
			set { _maxWidth = value; }
		}
		public int Minimum {
			get {
				int ret = 0;
				for (int i = 0; i < this.Count; i++) {
					ret += this[i].Maximum;
				}
				return ret;
			}
		}
		public int Height {
			get { return 20; }
		}
		public Header Hover {
			get {
				for (int i = 0; i < this.Count; i++) {
					if (this[i].Hover) {
						return this[i];
					}
				}
				return null;
			}
		}

		#endregion
	}


	internal enum HeaderType {
		Nr,
		Track,
		Time,
		Rating
	}
	internal enum HeaderSort {
		Default,
		Desc,
		Asc
	}
}
